
package com.seekting.study;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorsStudy {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() throws Exception {
                Thread.sleep(4000);
                System.out.println("调用啦");
                return "xxx" + Thread.currentThread().getName();
            }
        };

        List<Future> list = new ArrayList<Future>();
        int size = 20;
        for (int i = 0; i < 20; i++) {
            Future<String> future = executorService.submit(callable);
            list.add(future);
        }
        try {

            // future.cancel(false);
            for (int i = 0; i < 20; i++) {
                Future<String> future = list.get(i);
                System.out.println("get");
                String result = future.get();
                System.out.println(result);
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
